package com.sky.controller.user;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetDefaultAddressDTO;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Tag(name = "C端地址簿接口")
@Validated
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    @Operation(summary = "新增地址")
    public Result<String> save(@RequestBody @Valid AddressBook addressBook) {
        log.info("新增地址：{}", addressBook);
        // 设置用户ID
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.addAddress(addressBook);
        return Result.success(MessageConstant.ADD_ADDRESS_BOOK_SUCCESS);
    }

    /**
     * 查询当前登录用户的所有地址信息
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "查询当前登录用户的所有地址信息")
    public Result<List<AddressBook>> list() {
        List<AddressBook> list = addressBookService.showAllAddressBook();
        return Result.success(list, MessageConstant.GET_ADDRESS_BOOK_SUCCESS);
    }

    /**
     * 根据地址id设置默认地址
     * @param setDefaultAddressDTO
     * @return
     */
    @PutMapping("/default")
    @Operation(summary = "设置默认地址")
    public Result<String> setDefault(@RequestBody SetDefaultAddressDTO setDefaultAddressDTO) {
        log.info("设置默认地址：{}", setDefaultAddressDTO.getId());
        addressBookService.setDefaultAddress(setDefaultAddressDTO.getId());
        return Result.success(MessageConstant.SET_DEFAULT_ADDRESS_SUCCESS);
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    @Operation(summary = "查询默认地址")
    public Result<AddressBook> getDefault() {
        AddressBook addressBook = addressBookService.getDefaultAddress();
        return Result.success(addressBook,MessageConstant.GET_DEFAULT_ADDRESS_SUCCESS);
    }

    /**
     * 根据地址id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询地址")
    public Result<AddressBook> getById(@PathVariable @NotNull(message = "地址id不能为空") Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook,MessageConstant.GET_ADDRESS_SUCCESS);
    }

    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    @Operation(summary = "根据id修改地址")
    public Result<String> update(@RequestBody @Valid AddressBook addressBook) {
        log.info("修改地址信息：{}", addressBook);
        addressBookService.updAddress(addressBook);
        return Result.success(MessageConstant.UPDATE_ADDRESS_SUCCESS);
    }
    /**
     * 根据id删除地址
     */
    @DeleteMapping({"", "/"})//同时支持有尾部斜杠和无尾部斜杠的路径
    @Operation(summary = "根据id删除地址")
    public Result<String> delete(@RequestParam @NotNull(message = "地址id不能为空") Integer id) {
        log.info("删除地址id为{}", id);
        addressBookService.removeAddressById(id);
        return Result.success(MessageConstant.DELETE_ADDRESS_SUCCESS);
    }



}
